package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    //物资资料分页列表展示
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, String name, String categoryId, String supplier, Integer status, long[] categorys) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Product.class);
        Example.Criteria criteria = o.createCriteria();
        if (name != null && !"".equals(name)){
            criteria.andLike("name","%" + name + "%");
        }
        if (categorys == null){

        }else {
            if ( categorys.length > 0){
                switch (categorys.length){
                    case 1:
                        criteria.andEqualTo("oneCategoryId",categorys[categorys.length-1]);break;
                    case 2:
                        criteria.andEqualTo("twoCategoryId",categorys[categorys.length-1]);break;
                    case 3:
                        criteria.andEqualTo("threeCategoryId",categorys[categorys.length-1]);break;
                }
            }
        }

        if (status != null && !"".equals(status)){
            criteria.andEqualTo("status",status);
        }
        List<ProductVO> productVOList = productMapper.selectByExample(o).stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product,productVO);
            return productVO;
        }).collect(Collectors.toList());

        PageInfo<ProductVO> info = new PageInfo<>(productVOList);
        return new PageVO<>(info.getTotal(),productVOList);
    }

    //添加物资
    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setPNum(UUID.randomUUID().toString());
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(product.UNCHECK);
        product.setOneCategoryId(productVO.getCategoryKeys()[0]);
        product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        productMapper.insert(product);
    }

    //编辑物资
    @Override
    public ProductVO edit(Long id) {
        Product productById = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(productById,productVO);
        return productVO;
    }

    //修改物资
    @Override
    public void update(Long id,ProductVO productVO) {
        Product productById = productMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(productVO,productById);
        productById.setModifiedTime(new Date());
        productById.setOneCategoryId(productVO.getCategoryKeys()[0]);
        productById.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        productById.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        productMapper.updateByPrimaryKey(productById);
    }

    //物资回收站
    @Override
    public void remove(Long id) {
        Product productById = productMapper.selectByPrimaryKey(id);
        productById.setStatus(productById.DELETE);
        productMapper.updateByPrimaryKey(productById);
    }

    //物资恢复
    @Override
    public void back(Long id) {
        Product productById = productMapper.selectByPrimaryKey(id);
        productById.setStatus(productById.NORMAL);
        productMapper.updateByPrimaryKey(productById);
    }

    //物资删除
    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    //物资审核
    @Override
    public void publish(Long id) {
        Product productById = productMapper.selectByPrimaryKey(id);
        productById.setStatus(productById.NORMAL);
        productMapper.updateByPrimaryKey(productById);
    }


}
